package com.hyk.aop;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class Cglib implements MethodInterceptor {

    private Object object;

    public Cglib(Object object){
        this.object = object;
    }


    public Object getProxy(){
        Enhancer enhancer=new Enhancer();
//设置父类 class
        enhancer.setSuperclass(object.getClass());
//设置回调对象实现接口的类
        enhancer.setCallback(this);
        return enhancer.create();
    }


    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("hello");
        method.invoke(object,objects);
        System.out.println("hi");
        return null;
    }

}
